include('shared.lua')

function ENT:Initialize()
	self.vent = ClientsideModel("models/crow.mdl")
	self.vcycle = 0
	self.lastdraw = UnPredictedCurTime()
end

function ENT:OnRemove()
	self.vent:Remove()
end

function ENT:Draw()
	local delta = UnPredictedCurTime()-self.lastdraw
	self.lastdraw = UnPredictedCurTime()
	
	local pl = self.Entity:GetNWEntity("pl") or LocalPlayer()
	if not (IsValid(pl) and pl:IsPlayer()) then
		pl = LocalPlayer()
	end
	
	//FIRST KEY
	/*
	0 - Wings high
	1 - Flat
	2 - Foot out
	3 - Look left
	4 - Peck
	5 - Look forward flat
	6 - Look forward up
	7 - Glide
	8 - Roll right
	9 - Off origin back
	10 - Look forward cock left
	11 - 10 but lower origin
	12 - Prepare to land talons out
	13 - Walk step forward
	14 - Prepare to land talons in
	15 - None
	*/
	
	//ANIMS
	/*
	0 - Fly level
	1 - Idle
	2 - Walk forward
	3 - Run forward, look left
	4 - Peck worm, eat
	5 - Interested, worm
	6 - Take off
	7 - Glide
	8 - From glide, land on branch
	9 - Land, moving origin
	10 - Land, flat area
	11 - Jump forward, flat area, moving origin
	12 - Flat, wingspread, no anim
	13 - Take off, smack into something, die, moving origin
	14 - Flat, wingspread, talons in, no anim
	15 - None
	*/
	
	local vel = self.Entity:GetVelocity()
	
	local tr = util.TraceLine{
		start = self.Entity:GetPos(),
		endpos = self.Entity:GetPos() - Vector(0,0,16),
		filter = self.Entity,
	}
	
	local ground = tr.Hit
	local rate = 1
	
	local setang = true
	if vel:Length() < 16 and not ground then
		local ang = pl:EyeAngles()
		ang.p = -15
		self.vent:SetAngles(ang)
		self.vent:SetSequence(0)
		rate = .7
		
		setang = false
	elseif ground then
		if vel:Length() > 90 then
			local ang = vel:Angle()
			self.vent:SetAngles(ang)
			rate = 2
			
			self.vent:SetSequence(3)
		elseif vel:Length() > 7 then
			local ang = vel:Angle()
			self.vent:SetAngles(ang)
			rate = 1
			
			self.vent:SetSequence(2)
		else
			local ang = self.vent:EyeAngles()
			ang.p = 0
			self.vent:SetAngles(ang)
			
			self.vent:SetSequence(1)
			rate = .1
		end
		
		setang = false
	elseif vel.z > 2 then
		self.vent:SetSequence(0)
	elseif vel:Length() < 16 and ground then
	else
		self.vent:SetSequence(7)
	end
	
	if setang then
		self.vent:SetAngles(pl:EyeAngles())
	end
	
	self.vent:SetPos(self.Entity:GetPos()-Vector(0,0,8))
	
	self.vcycle = self.vcycle + delta*rate
	if self.vcycle > 1 then
		self.vcycle = self.vcycle - 1
	end
	self.vent:SetCycle(self.vcycle)
	self.vent:DrawModel()
end

function ENT:Think()
end